package com.xingshang.config;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 支付宝配置类
 * 用于配置支付宝客户端所需的参数
 */
@Configuration
public class AlipayConfig {
    /**
     * 支付宝网关
     * 沙箱环境使用：https://openapi.alipaydev.com/gateway.do
     * 正式环境使用：https://openapi.alipay.com/gateway.do
     */
    private static final String URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    /**
     * 应用ID
     * 在支付宝开放平台创建应用后获取
     */
    private static final String APP_ID = "2021000148663057";

    /**
     * 商户私钥
     * 使用支付宝开放平台提供的工具生成
     * 注意：需要妥善保管，不要泄露
     */
    private static final String PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCbpvAG+vSeuCl3Snlcx5lHHekkpbijkWVCJnCdKX93hRT5XXgcaLkuoRjhbcOF1Wd3ZFN/cXUQ0uDFWjndHCdNycorEOIYohb/TMuOGJVSPlQpVtBOfhSMcSmhB3FT505O7ZfWh0yboUzinuHcKpiXHqE/ik2MSZle1TUHeiUTdlOiNo2TBUWy73SYXr4RBnTIdtQuWXV2lpVnbYS5zgDDtNuX6jYWX2+epzjlQnjIGMZfYt5Kmp8Lp4tY8HsjC3jGvix0QG0BcL3JBEHaDoRKAXcRfRy598xvJru9tfBgIR3DZrgMX3A1M9EDOrNcKARn/1vqQ1oYY8dZr4mXVCWJAgMBAAECggEAJ66TmvP30U8aIzN1ZVXtmdfiTFmDjQ/QROEP7MFbodjn0/skV/nK3Dxt32rgYgpZ6nJFZ0RBmBxq+WVbQyeeTZKYXApd2fHY8xVc/8j8qI8DXdaoHJrAGl3vQwJmvgFGUZD5GNQ9j5+7vjrLUhn59Oz2JwDXbGCHHUo1zsYxFZi6/zoFwX3IPjZ+WkqaXMPIInkAmyt8NIt1WnVkyZpSzGQIi+Oh0FPGR+AyPRyoJBc14JIt5ZKPHM5HwkD5j6OP6W7BmV6ppwFQ+eQIOTdoKe+T5LgQxoF7cCqwYJguxDCVnjpXZPAlm0oBPZjFZyzpwtkGmbA8AG2UJgPm+DGFUQKBgQDXQoSpjO6XL9rMm8WWgWbCC19B5xl3F77KDbQNuF21LPOHhS1XC+UtwcQhXk4eDzAXj/1HgJlD05fxgWSUlTLx02nXquOHxEUhrCEmq+QgxZiW/zRAn3rGSqa9quG2jb5YroC+HfFpbbH2ty0DbKR5zi0xDNDQGpPjpIjgBEJGdwKBgQC5HGKRqP/LcKcA16B/w5/eR6WW/q7+6+7e7usuVVsFrdE/ug0h6CHHWn1SSEYX6CwCGpiqWACr8e0WT0rBGQN/NoV/v15xUVmTe77aQdg93aYioCq+laSGZKLLtK6a1cRAWZpScTm1dXLUPFU5xezR4eoMLe+5esEMiBqDJTvz/wKBgQDF0negacgKYQwu5Sjce+c3hMPyCLO6YrQPdDB4O2rCyaC0douIZ4IiXuQj1krsDQEhZk5Zm7E+SAJnC4anlcXPv36RmJapepIds9Zzd5ybEXx9p+MZUHbALHK384ajAfhf/16JWXM2dH0MQqK3duxglD2ryh9MYGBw4BB57vAJWQKBgC5Y/hEMaGduoFHkq0nyGCAWfk5tHDG1rwxgc8AZHP4yBBdZpRodPf/lF0W31Nz9g69PK3RTLm/e1gbk+YmZkreSViCjMKlcvfngXOvW2qa6zXMT+T/YxeXkYqJwbdtUtJuME8iakuFtM8ASDyC0/tOaJfjAs6oUIyvZFBJMC1PPAoGBAMTibBJGlPUJtcf/5asuw/sXdqEy9zOI0WHFOJhBCgW7PR7sSOcy4wWG4njcuZskfCxtPoh3g/VXkFfouxQTZXAK5eq7nTjxUixJYbo8xWYSA5xwxYC5ms3bfYm8qv2SdC5zlJFBZSQ2SAbGTn4w47pHcRr1ZPzxjQSJXzF2jyWf";

    /**
     * 支付宝公钥
     * 在支付宝开放平台获取
     * 用于验证支付宝的异步通知
     */
    private static final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv2rf9apKEup8s//H5OZGUbZo3Jiiz5zaHfrwg5xKNVdisdC8JyU8r3a5w+embZs9vO2509CoiVw6NqHyMMhxz3bo4VmX47LDUNszTNau2Xvpdqp5kDGN7rGxMxnERxHGkxhaZ6aI8dD0Hhc2gfzwLTp+cRKwrrMt9Li1ZEXEr/fCEjlHiFm4bg03Al5nyM2bC0G1ixwIbbhebUVh3Nm461wRQxXRazbVInbIH+g3zkOd6pnHRdSeZTHVLIS8J6BCyeYLFiES7EWRp/NolRWiUa4rnKycDRWR6FYH0X2zDvS3gNC0KI3RaJRUMIvlIdJ0tZj64SfTpIKvwsx9V+c1PQIDAQAB";

    /**
     * 异步通知地址
     * 支付宝服务器主动通知商户服务器里指定的页面http/https路径
     * 建议使用https协议,必须是外网地址，不然支付宝服务器无法返回支付是否成功
     */
    private static final String NOTIFY_URL = "http://localhost:8080/alipay/notify";

    /**
     * 同步返回地址
     * 支付完成后，用户从支付宝页面跳转回商户页面的地址
     */
    private static final String RETURN_URL = "http://localhost:8080/alipay/return";
    /**
     * 创建支付宝客户端
     * @return AlipayClient实例
     */
    @Bean
    public AlipayClient alipayClient() {
        return new DefaultAlipayClient(
                URL,                // 支付宝网关
                APP_ID,            // 应用ID
                PRIVATE_KEY,       // 商户私钥
                "json",           // 请求格式
                "UTF-8",          // 编码格式
                ALIPAY_PUBLIC_KEY, // 支付宝公钥
                "RSA2"            // 签名方式
        );
    }
}
